package com.xiaoshuidi.cloud.module.member.mq.consumer;

import com.xiaoshuidi.cloud.framework.common.util.json.JsonUtils;
import com.xiaoshuidi.cloud.module.bpm.enums.BpmBusinessTypeEnum;
import com.xiaoshuidi.cloud.module.bpm.mq.message.BpmProcessInstanceResultEvent;
import com.xiaoshuidi.cloud.module.member.repository.MemberCouponRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Slf4j
@Component
public class BpmProcessInstanceResultConsumer implements ApplicationListener<BpmProcessInstanceResultEvent> {
    @Resource
    private MemberCouponRepository memberCouponRepository;

    @Override
    public void onApplicationEvent(BpmProcessInstanceResultEvent event) {
        log.info("收到工作流状态变更消息{}", JsonUtils.toJsonString(event));
        //是优惠券的消息才处理
        if (BpmBusinessTypeEnum.COUPON.name().equals(event.getBusinessType())){
            log.info("处理审批结果 {} {}", event.getBusinessKey(), event.getResult());
            memberCouponRepository.updateStatusByBpmResult(event.getTenantId(), event.getBusinessKey(), event.getResult());
        }

    }
}
